/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import base.*;
import java.util.Hashtable;
import java.util.Vector;
import trackmsg.EchoMsg;
import trackmsg.TrackCtrlMsg;

/**
 *
 * @author FUJISAI
 */
public class ASTAssignRightSubExecution extends WaveExecution{

    public ASTAssignRightSubExecution(WaveHeadParser parser, WaveEnvironment waveEnv) {
        super(parser, waveEnv);
    }

    public void execute(ASTAssignSubscript asgnSub, String tail) {
        SimpleNode left = (SimpleNode)asgnSub.jjtGetChild(0);
        SimpleNode right = (SimpleNode)asgnSub.jjtGetChild(1);

        SimpleNode l_left = (SimpleNode)left.jjtGetChild(0);
        SimpleNode l_right = (SimpleNode)left.jjtGetChild(1);

        Vector v_right, v_index = new Vector();
        
        v_index = nodeToVector(right);
        v_right = nodeToVector(l_right);

        Hashtable fTab = this.waveEnv.getFrontalTable();

        if (l_left instanceof ASTFrontalVar) {
            Vector v = new Vector();
            addElementAtIndexes(v, v_index, v_right);
            fTab.put(l_left.getString(), v);
            //Utilities.WPPrint(fTab);
        }
        else if (l_left instanceof ASTNodalVar) {
            Hashtable nTab = this.kn.getKNNode(
                        this.waveEnv.getCONTENT()).getNodalTable(waveEnv.getColor());
            Vector v = new Vector();
            addElementAtIndexes(v, v_index, v_right);
            nTab.put(l_left.getString(), v);
            //Utilities.WPPrint(nTab);
        }

        if (waveEnv.getTrackAddress() != null && tail.equals("")) {
            //Neu tail ma rong thi gui echo la TRUE di
            TrackNode fromTrack = wi.getTrackForest().getTrackNode(waveEnv.getTrackAddress());
            TrackNode.Echo echo = TrackNode.Echo.TRUE;
            //Tao message echo va gui vao TrackQueue
            TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
            wi.getTrackQueue().incoming(msg);
        } else if(!tail.equals("")){
            Wave newWave = new Wave(tail, new WaveEnvironment(waveEnv));
            waveQueue.incomingFront(newWave);
        }
        waveEnv.dump();
        Utilities.WPPrint("Executed Assign Right Subscript Done!");
    }

    public void addElementAtIndexes(Vector dVec, Vector index, Vector sVec) {
        for (int i=0; i<index.size(); i++) {
            int sVec_index = Integer.parseInt(index.elementAt(i).toString());
            Utilities.WPPrint("INDEX" + sVec_index);
            dVec.add(sVec.elementAt(sVec_index-1));
        }
    }

    public Vector nodeToVector(SimpleNode node){
        Vector vector = new Vector();

        if (node instanceof ASTArrayConst) {
            for (int i=0;i<node.jjtGetNumChildren(); i++) {
                SimpleNode child_node = (SimpleNode)node.jjtGetChild(i);

                vector.addAll(getASTNodeValue(child_node));
            }
        }
        else
            vector = getASTNodeValue(node);
        return vector;
    }

    public boolean isNum (String str) {
        try{
            Integer.parseInt(str);
        } catch(NumberFormatException nfe) {
            return false;
        }
        return true;
    }

}

